{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PubChemPy examples\n",
    "\n",
    "## Table of Contents\n",
    "\n",
    "- [1. Introduction](1-introduction.ipynb)\n",
    "- [2. Getting Started](2-getting-started.ipynb)\n",
    "\n",
    "# 1. Introduction\n",
    "\n",
    "PubChemPy provides a way to interact with PubChem in Python. It allows chemical searches by name, substructure and similarity, chemical standardization, conversion between chemical file formats, depiction and retrieval of chemical properties.\n",
    "\n",
    "Here’s a quick example showing how to search for a compound by name:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5793\n",
      "C([C@@H]1[C@H]([C@@H]([C@H](C(O1)O)O)O)O)O\n",
      "79025\n",
      "C([C@@H]1[C@H]([C@@H]([C@H]([C@H](O1)O)O)O)O)O\n",
      "64689\n",
      "C([C@@H]1[C@H]([C@@H]([C@H]([C@@H](O1)O)O)O)O)O\n",
      "206\n",
      "C(C1C(C(C(C(O1)O)O)O)O)O\n"
     ]
    }
   ],
   "source": [
    "from pubchempy import get_compounds\n",
    "\n",
    "for compound in get_compounds('glucose', 'name'):\n",
    "    print(compound.cid)\n",
    "    print(compound.isomeric_smiles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So how does this work behind the scenes?\n",
    "\n",
    "1. We call the PubChemPy function `get_compounds` with the parameters `'glucose'` and `'name'`\n",
    "2. This is translated into [a request](https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/glucose/JSON) for the PubChem PUG REST API.\n",
    "3. PubChemPy parses the JSON response into a list of `Compound` objects.\n",
    "4. Each `Compound` has properties like `cid` and `isomeric_smiles`, which we print.\n",
    "\n",
    "Here’s how you get calculated properties for a specific compound:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C17H14O4S\n",
      "314.35566\n",
      "2.3\n"
     ]
    }
   ],
   "source": [
    "from pubchempy import Compound\n",
    "\n",
    "vioxx = Compound.from_cid(5090)\n",
    "print vioxx.molecular_formula\n",
    "print vioxx.molecular_weight\n",
    "print vioxx.xlogp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When using PubChemPy, it is important to remember that every request you make is transmitted to the PubChem servers, evaluated, and then a response is sent back. There are some downsides to this: It is less suitable for confidential work, it requires a constant internet connection, and some tasks will be slower than if they were performed locally on your own computer. On the other hand, this means we have the vast resources of the PubChem database and chemical toolkits at our disposal. As a result, it is possible to do complex similarity and substructure searching against a database containing tens of millions of compounds in seconds, without needing any of the storage space or computational power on your own local computer.\n",
    "\n",
    "You don’t need to worry too much about how the PubChem web service works, because PubChemPy handles all of the details for you. But if you want to go beyond the capabilities of PubChemPy, there is some [helpful documentation on the PubChem website](https://pubchem.ncbi.nlm.nih.gov/pug_rest/PUG_REST_Tutorial.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "Next: [Getting Started](2-getting-started.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
